package com.sigmatrix.h5.dao.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;
import org.springframework.util.CollectionUtils;

import com.sigmatrix.h5.dao.PackStandardDao;
import com.sigmatrix.h5.dto.PackStandardData;
import com.sigmatrix.h5.entity.PackStandard;
import com.sigmatrix.h5.exception.DaoException;
import com.sigmatrix.h5.utils.Cons;
import com.sigmatrix.h5.utils.Utils;
import com.sigmatrix.sc.jdbc.common.dao.DaoImpl;

/**
 * 
 *@ClassName: PackStandardDaoImpl
 *@Description: 包装规格
 *@author MH
 *@date 2017年9月27日 上午9:29:42
 */
@Repository(value="packStandardDao")
public class PackStandardDaoImpl extends DaoImpl<PackStandard, Integer> implements PackStandardDao{

	/**表名*/
	public static final String TABLE_NAME = "_pack_standard";

	/**
	 * (non-Javadoc)
	 *@Title: findById
	 *@Description: 
	 *@see com.sigmatrix.sc.handheld.inter.dao.PackStandardDao#findById(java.lang.Integer)
	 *@author youjun
	 *@date 2017年6月26日 下午2:28:50
	 */
	@Override
	public PackStandard findById(final Integer id, final String enterpriseCode) throws DaoException {
		final StringBuilder sql = new StringBuilder("SELECT * FROM ").append(getTname(enterpriseCode, TABLE_NAME));
		sql.append(" WHERE _id = ?");
		final Object[] params = new Object[]{id};
		final List<PackStandard> list = jdbcTemplate.query(sql.toString(), params, new PackStandardRowMapper());
		if(CollectionUtils.isEmpty(list)) {
			return null;
		}
		return list.get(0);
	}
	
	/**(non-Javadoc)
	 *@Title: findPartColumnListByCondition
	 *@Description: 
	 *@see com.sigmatrix.sc.handheld.inter.dao.PackStandardDao#findPartColumnListByCondition(com.sigmatrix.sc.handheld.inter.entity.PackStandard)
	 *@author youjun
	 *@date 2017年6月26日 下午2:28:33
	 */
	@Override
	public List<PackStandardData> findPartColumnListByCondition(final PackStandard packStandard, final String enterpriseCode) throws DaoException {

		final StringBuilder sql = new StringBuilder("SELECT _level, _name, _sub_num FROM ").append(getTname(enterpriseCode, TABLE_NAME));
		sql.append(" WHERE _status <> ?");
		final List<Object> paramList = new ArrayList<Object>();
		paramList.add(Cons.STATUS_DELETE);
		sql.append(assemblyWhereConditon(packStandard, paramList, true));
		sql.append(" ORDER BY _level ASC");
		return jdbcTemplate.query(sql.toString(), paramList.toArray(), new RowMapper<PackStandardData>() {

			@Override
			public PackStandardData mapRow(final ResultSet resultSet, final int rowNum) throws SQLException {
				
				final PackStandardData packStandard = new PackStandardData();
				packStandard.setName(resultSet.getString("_name"));
				packStandard.setLevel(resultSet.getShort("_level"));
				packStandard.setSubNum(resultSet.getInt("_sub_num"));
	            return packStandard;
			}
		});
	}
	
	/**
	 *@Title: assemblyWhereConditon
	 *@Description: TODO()
	 *@return Object
	 *@author youjun
	 *@date 2017年6月26日 下午2:33:13
	 *@param packStandard
	 *@param paramList
	 *@param isExistWhereCondition
	 *@return
	 * @throws DaoException 
	 */
	private String assemblyWhereConditon(final PackStandard packStandard, final List<Object> paramList, final boolean isExistWhereCondition) throws DaoException {

		if(paramList==null) {
			throw new DaoException("参数列表不能为null");
		}
		StringBuilder whereSql = new StringBuilder();
		if(!isExistWhereCondition) {
			whereSql.append(" WHERE 1=1");
		}
		if(packStandard!=null) {
			if(packStandard.getProductId()!=null) {
				whereSql.append(" AND _product_id = ?");
				paramList.add(packStandard.getProductId());
			}
		}
		return whereSql.toString();
	}

	/**
	 * (non-Javadoc)
	 *@Title: findListByCondition
	 *@Description: 
	 *@see com.sigmatrix.sc.handheld.inter.dao.PackStandardDao#findListByCondition(com.sigmatrix.sc.handheld.inter.entity.PackStandard)
	 *@author youjun
	 *@date 2017年6月26日 下午2:28:47
	 */
	@Override
	public List<PackStandard> findListByCondition(final PackStandard packStandard, final String enterpriseCode) throws DaoException {

		final StringBuilder sql = new StringBuilder("SELECT * FROM ").append(getTname(enterpriseCode, TABLE_NAME));
		final List<Object> paramList = new ArrayList<Object>();
		sql.append(" WHERE _status <> ?");
		paramList.add(Cons.STATUS_DELETE);
		sql.append(assemblyWhereConditon(packStandard, paramList, true));
		sql.append(" ORDER BY _level ASC");
		try {
			return jdbcTemplate.query(sql.toString(), paramList.toArray(), new PackStandardRowMapper());
		} catch (DataAccessException e) {
			throw new DaoException(e);
		}
	}
	
	/**
	 *@ClassName: PackStandardRowMapper
	 *@Description: 封装返回的实体
	 *@author youjun
	 *@date 2017-06-24 18:52:13
	 */
	public class PackStandardRowMapper implements RowMapper<PackStandard> {

		/**(non-Javadoc)
		 *@Title: mapRow
		 *@Description: 
		 *@see 
		 *@author youjun
		 *@date 2017-06-24 18:52:13
		 */
		@Override
		public PackStandard mapRow(final ResultSet resultSet, final int rowNum) throws SQLException {
			final PackStandard packStandard = new PackStandard();
            packStandard.setId(resultSet.getInt("_id"));
			packStandard.setProductId(resultSet.getInt("_product_id"));
			packStandard.setProductCode(resultSet.getString("_product_code"));
			packStandard.setProductName(resultSet.getString("_product_name"));
			packStandard.setProductNormsValue(resultSet.getInt("_product_norms_value"));
			packStandard.setProductNormsUnit(resultSet.getString("_product_norms_unit"));
			packStandard.setName(resultSet.getString("_name"));
			packStandard.setLevel(resultSet.getShort("_level"));
			packStandard.setSubNum(resultSet.getInt("_sub_num"));
			packStandard.setRemarks(resultSet.getString("_remarks"));
			packStandard.setCreatorId(resultSet.getInt("_creator_id"));
			packStandard.setCreateTime(resultSet.getTimestamp("_create_time"));
			packStandard.setUpdateId(resultSet.getInt("_update_id"));
			packStandard.setUpdateTime(resultSet.getTimestamp("_update_time"));
			packStandard.setEnableId(resultSet.getInt("_enable_id"));
			packStandard.setEnableTime(resultSet.getTimestamp("_enable_time"));
			packStandard.setDisableId(resultSet.getInt("_disable_id"));
			packStandard.setDisableTime(resultSet.getTimestamp("_disable_time"));
			packStandard.setStatus(resultSet.getShort("_status"));
            return packStandard;
		} 
	}

	/**是否为分表策略,为true,表名加上传递的企业code*/
	public static final boolean ISSUBMETER = true;
	
	/**
	 *@Title: getEntTableName
	 *@Description: 根据表策略(分库/表)获得表名
	 *@return String
	 *@date 2017年7月13日 上午11:52:48
	 *@param eseCode
	 *@param tName
	 */
	protected String getTname(final String eseCode, final String tName) {
		
		final StringBuilder sb = new StringBuilder();
		if(ISSUBMETER) { // 分表
			if(Utils.isNotEmpty(eseCode)){//企业code不为空
				sb.append("_").append(eseCode);
			}
		}
		sb.append(tName);
		return sb.toString();
	}
}
